# _init__.py
import os
import smtplib

from flask_cors import CORS
from flask_mail import Mail, Message
from flask import Flask
from module_admin.controller import *
from module_admin.controller.discussion import discussion
from module_admin.controller.submission_controller import submission

from module_admin.entity.do import *
from module_admin.entity.do import comment
from module_admin.controller.assignment_controller import assignment
from module_admin.controller.user import user
from module_admin.controller.teacher import teacher
from module_admin.controller.student import student
from module_admin.controller.course import course
from module_admin.controller.content import content
from module_admin.controller.test import test
from module_admin.controller.upload import upload


# 定义注册蓝图方法
def register_blueprints(app):
    app.register_blueprint(assignment)
    app.register_blueprint(user)
    app.register_blueprint(teacher)
    app.register_blueprint(student)
    app.register_blueprint(course)
    app.register_blueprint(content)
    app.register_blueprint(upload)
    # app.register_blueprint(submission)
    # app.register_blueprint(Assignment.AssBP, url_prefix='/Assignment')
    # app.register_blueprint(CourseManagement.courseManagementBP, url_prefix='/CourseManagement')
    # app.register_blueprint(UserManagement.userManagementBP, url_prefix='/UserManagement')
    app.register_blueprint(test)
    app.register_blueprint(discussion)
    # app.register_blueprint(Discussion.discussionBP, url_prefix='/Discussion')


# 注册插件(数据库关联)
def register_plugin(app):
    from module_admin.entity.do.base import db
    db.init_app(app)
    # create_all要放到app上下文环境中使用
    with app.app_context():
        print(1112)
        db.create_all()


def create_app():
    # app = Flask(__name__,template_folder='/templates')
    app = Flask(os.path.join(os.path.dirname(__file__), ".."))
    # app.config.from_object('app.config.setting') # 基本配置(setting.py) 
    app.config.from_object('config')  # 重要参数配置(secure.py)
    app.secret_key = 'your_secret_key'
    app.config.update(
        MAIL_SERVER='smtp.163.com',
        MAIL_PORT=25,
        MAIL_USE_TLS=True,
        MAIL_USERNAME='zj2585306788@163.com',
        MAIL_PASSWORD='ZTLVGCSLEQILTDVS'
    )

    # Initialize Flask-Mail
    mail = Mail(app)
    # 注册蓝图与app对象相关联
    register_blueprints(app)
    # 注册插件(数据库)与app对象相关联
    register_plugin(app)
    CORS(app)  # 允许所有的域名跨域请求
    # 一定要记得返回app
    return app, mail


#
# from sqlalchemy import func
#
#
# @assignment.route('/students/performance', methods=['GET'])
# def students_performance():
#     # 计算所有类型为 'test' 的文件数量
#     total_files_count = File.query.filter(File.type == 'test').count()
#
#     # 如果没有文件，直接返回
#     if total_files_count == 0:
#         return jsonify({"message": "No test files found", "data": []}), 200
#
#     # 获取所有学生的提交数量，并计算百分比
#     results = (
#         db.session.query(
#             Submission.student_name,
#             func.count(Submission.id).label("submission_count")
#         )
#         .group_by(Submission.student_name)
#         .all()
#     )
#
#     # 构造结果
#     data = []
#     for result in results:
#         student_name, submission_count = result
#         # 计算百分比
#         percentage = (submission_count / total_files_count) * 100
#         # 确保百分比不超过100
#         percentage = min(percentage, 100)
#         data.append({"student_name": student_name, "percentage": percentage})
#
#     return jsonify({"data": data}), 200